"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var fs = require("fs");
var ScriptSettings = /** @class */ (function () {
    function ScriptSettings() {
        this.v2Backup = JSON.parse(fs.readFileSync('./listen1_backup.json', 'utf-8'));
        this.setting = {
            language: this.v2Backup.language,
            theme: this.v2Backup.theme,
            playerSetting: this.v2Backup["player-settings"],
        };
        this.v3DefaultSettings = [
            { key: "enableAutoChooseSource", value: false },
            { key: "enableGlobalShortCut", value: false },
            { key: "enableLyricFloatingWindow", value: false },
            { key: "enableLyricFloatingWindowTranslation", value: false },
            { key: "enableLyricTranslation", value: false },
            { key: "enableNowplayingBitrate", value: false },
            { key: "enableNowplayingComment", value: false },
            { key: "enableNowplayingCoverBackground", value: false },
            { key: "enableNowplayingPlatform", value: false },
            { key: "enableStopWhenClose", value: true },
            { key: "favorite_playlist_order", value: [] },
            { key: "language", value: "zh-CN" },
            { key: "lyricFontSize", value: 15 },
            { key: "lyricFontWeight", value: 400 },
            {
                key: "my_playlist_order",
                value: [
                    "myplaylist_redheart",
                    "myplaylist_75390912-a84c-4d74-87d5-457ee420e950",
                ],
            },
            { key: "playerSettings", value: {} },
            { key: "theme", value: "black" },
        ];
        this.version = "3";
    }
    ScriptSettings.prototype.getCurrentPlayinglistTracks = function () {
        var tracks = this.v2Backup["current-playing"];
        var formattedTracks = [];
        for (var _i = 0, tracks_1 = tracks; _i < tracks_1.length; _i++) {
            var track = tracks_1[_i];
            delete track.disabled;
            track.playlist = "current";
            formattedTracks.push(track);
        }
        return formattedTracks;
    };
    ScriptSettings.prototype.getMyPlaylistTrack = function () {
        var playerlists = this.v2Backup.playerlists;
        var formattedTracks = [];
        for (var _i = 0, playerlists_1 = playerlists; _i < playerlists_1.length; _i++) {
            var playlistId = playerlists_1[_i];
            var playlist = this.v2Backup[playlistId];
            var tracks = playlist.tracks;
            var playlistName = playlist.info.title;
            for (var _a = 0, tracks_2 = tracks; _a < tracks_2.length; _a++) {
                var track = tracks_2[_a];
                delete track.disabled;
                delete track.index;
                delete track.playNow;
                delete track.platformText;
                track.playlist = playlistId;
                formattedTracks.push(track);
            }
        }
        return formattedTracks;
    };
    ScriptSettings.prototype.getMyPlaylist = function () {
        var playerlists = this.v2Backup["playerlists"];
        var formattedLists = [];
        for (var i = 0; i < playerlists.length; i++) {
            var playlistId = playerlists[i];
            var playlistData = this.v2Backup[playlistId];
            var formattedPlaylist = {
                cover_img_url: playlistData.info.cover_img_url,
                title: playlistData.info.title,
                id: playlistData.info.id,
                source_url: playlistData.info.source_url,
                type: "my",
                order: playlistData.tracks.map(function (track) { return track.id; }),
            };
            formattedLists.push(formattedPlaylist);
        }
        return formattedLists;
    };
    ScriptSettings.prototype.getFavoritePlaylist = function () {
        var favoritePlayerLists = this.v2Backup.favoriteplayerlists || [];
        var formattedLists = [];
        for (var i = 0; i < favoritePlayerLists.length; i++) {
            var playlistId = favoritePlayerLists[i];
            var playlistData = this.v2Backup[playlistId];
            var formattedPlaylist = {
                id: playlistData.info.id,
                cover_img_url: playlistData.info.cover_img_url,
                title: playlistData.info.title,
                description: "",
                source_url: playlistData.info.source_url,
                type: "favorite",
            };
            formattedLists.push(formattedPlaylist);
        }
        return formattedLists;
    };
    ScriptSettings.prototype.getCurrentPlayinglist = function () {
        if (!this.v2Backup || !this.v2Backup["current-playing"]) {
            console.log("current-playing does not exist");
            return [];
        }
        var order = this.v2Backup["current-playing"].map(function (item) { return item.id; });
        var currentObject = {
            id: "current",
            title: "current",
            cover_img_url: "",
            type: "current",
            order: order,
        };
        return [currentObject];
    };
    ScriptSettings.prototype.getPlaylists = function () {
        var currentPlayingList = this.getCurrentPlayinglist();
        var myPlaylist = this.getMyPlaylist();
        var favoritePlaylist = this.getFavoritePlaylist();
        return currentPlayingList.concat(myPlaylist, favoritePlaylist);
    };
    ScriptSettings.prototype.setPlayerSetting = function () {
        var playerSettingsValue;
        for (var index = 0; index < this.v3DefaultSettings.length; index++) {
            if (this.v3DefaultSettings[index].key === "playerSettings") {
                playerSettingsValue = this.v3DefaultSettings[index].value;
                break;
            }
        }
        playerSettingsValue = this.setting.playerSetting;
    };
    ScriptSettings.prototype.setPlaylistOrder = function (playlists, v3DefaultSettings) {
        var myPlaylistIds = [];
        for (var _i = 0, playlists_1 = playlists; _i < playlists_1.length; _i++) {
            var playlist = playlists_1[_i];
            if (playlist.type === "my") {
                myPlaylistIds.push(playlist.id);
            }
        }
        for (var _a = 0, v3DefaultSettings_1 = v3DefaultSettings; _a < v3DefaultSettings_1.length; _a++) {
            var setting = v3DefaultSettings_1[_a];
            if (setting.key === "my_playlist_order") {
                setting.value = myPlaylistIds;
                break;
            }
        }
    };
    ScriptSettings.prototype.setCurrentPlayingTrack = function (id, tracks) {
        for (var i = 0; i < tracks.length; i++) {
            if (tracks[i].id === id) {
                tracks[i].highlight = true;
                break;
            }
        }
    };
    ScriptSettings.prototype.getTracks = function () {
        var myPlaylistTracks = this.getMyPlaylistTrack();
        var currentPlayinglistTracks = this.getCurrentPlayinglistTracks();
        var mergedTracks = myPlaylistTracks.concat(currentPlayinglistTracks);
        return mergedTracks;
    };
    ScriptSettings.prototype.configV3Backup = function () {
        var v3Backup = {
            Tracks: this.getTracks(),
            Settings: [],
            Playlists: this.getPlaylists(),
            version: "3",
        };
        this.setPlayerSetting();
        this.setCurrentPlayingTrack(this.setting.playerSetting.nowplaying_track_id, v3Backup["Tracks"]);
        this.setPlaylistOrder(v3Backup.Playlists, this.v3DefaultSettings);
        v3Backup.Settings = this.v3DefaultSettings;
        console.log(JSON.stringify(v3Backup));
        return v3Backup;
    };
    ScriptSettings.prototype.exportToJsonFile = function (data, filePath) {
        var jsonString = JSON.stringify(data, null, 2);
        fs.writeFile(filePath, jsonString, "utf-8", function (err) {
            if (err) {
                console.error("写入文件时发生错误:", err);
            }
            else {
                console.log("JSON 文件已成功导出。");
            }
        });
    };
    ScriptSettings.prototype.exportV3Backup = function () {
        var v3BackupData = this.configV3Backup();
        var filePath = "./listen1_backup_v3.json";
        this.exportToJsonFile(v3BackupData, filePath);
    };
    return ScriptSettings;
}());
// 使用示例
var scriptSettings = new ScriptSettings();
console.log(scriptSettings.setting);
console.log(scriptSettings.v3DefaultSettings);
console.log(scriptSettings.version);
scriptSettings.exportV3Backup();
